﻿using DBUtil;
using Function;
using Model.Permission;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace DAL.Permission.Shop
{
    public class ExtendedPermissionTypeDAL
    {
        DbHelperSQLP DbHelperSQL = new DbHelperSQLP(PubConstant.JHShop);
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ExtendedPermissionTypeID, string RightType)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [Permission].[ExtendedPermissionType]");
            strSql.Append(" where ExtendedPermissionTypeID!=@ExtendedPermissionTypeID and [RightType]=@RightType");
            SqlParameter[] parameters = {
					new SqlParameter("@ExtendedPermissionTypeID", ExtendedPermissionTypeID),
                    new SqlParameter("@RightType",RightType)
			};

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string RightType)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [Permission].[ExtendedPermissionType]");
            strSql.Append(" where [RightType]=@RightType");
            SqlParameter[] parameters = {
                    new SqlParameter("@RightType",RightType)
			};

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(ExtendedPermissionTypeEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into [Permission].[ExtendedPermissionType](");
            strSql.Append("RightType,Note)");
            strSql.Append(" values (");
            strSql.Append("@RightType,@Note)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@RightType", SqlDbType.NVarChar,200),
					new SqlParameter("@Note", SqlDbType.NVarChar,200)};
            parameters[0].Value = model.RightType;
            parameters[1].Value = model.Note;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ExtendedPermissionTypeEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update [Permission].[ExtendedPermissionType] set ");
            strSql.Append("RightType=@RightType,");
            strSql.Append("Note=@Note");
            strSql.Append(" where ExtendedPermissionTypeID=@ExtendedPermissionTypeID");
            SqlParameter[] parameters = {
					new SqlParameter("@RightType", SqlDbType.NVarChar,200),
					new SqlParameter("@Note", SqlDbType.NVarChar,200),
					new SqlParameter("@ExtendedPermissionTypeID", SqlDbType.Int,4)};
            parameters[0].Value = model.RightType;
            parameters[1].Value = model.Note;
            parameters[2].Value = model.ExtendedPermissionTypeID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ExtendedPermissionTypeID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [Permission].[ExtendedPermissionType] ");
            strSql.Append(" where ExtendedPermissionTypeID=@ExtendedPermissionTypeID");
            SqlParameter[] parameters = {
					new SqlParameter("@ExtendedPermissionTypeID", SqlDbType.Int,4)
			};
            parameters[0].Value = ExtendedPermissionTypeID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string ExtendedPermissionTypeIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [Permission].[ExtendedPermissionType] ");
            strSql.Append(" where ExtendedPermissionTypeID in (" + ExtendedPermissionTypeIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ExtendedPermissionTypeEntity GetModel(int ExtendedPermissionTypeID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ExtendedPermissionTypeID,RightType,Note from [Permission].[ExtendedPermissionType] ");
            strSql.Append(" where ExtendedPermissionTypeID=@ExtendedPermissionTypeID");
            SqlParameter[] parameters = {
					new SqlParameter("@ExtendedPermissionTypeID", SqlDbType.Int,4)
			};
            parameters[0].Value = ExtendedPermissionTypeID;

            ExtendedPermissionTypeEntity model = new ExtendedPermissionTypeEntity();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ExtendedPermissionTypeEntity DataRowToModel(DataRow row)
        {
            ExtendedPermissionTypeEntity model = new ExtendedPermissionTypeEntity();
            if (row != null)
            {
                if (row["ExtendedPermissionTypeID"] != null && row["ExtendedPermissionTypeID"].ToString() != "")
                {
                    model.ExtendedPermissionTypeID = int.Parse(row["ExtendedPermissionTypeID"].ToString());
                }
                if (row["RightType"] != null)
                {
                    model.RightType = row["RightType"].ToString();
                }
                if (row["Note"] != null)
                {
                    model.Note = row["Note"].ToString();
                }
            }
            return model;
        }
    }
}
